package org.cosmact.designer.vxml.gefeditor.editpart.command;

import org.cosmact.vxml.model.Dialog;
import org.cosmact.vxml.model.Document;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

public class CreateDialogCommand extends Command {

	/** The new shape. */
	private Dialog newDialog;
	/** ShapeDiagram to add to. */
	private final Document parentDocument;

	/** The bounds of the new dialog figure . */
	private Rectangle bounds;

	/**
	 * Create a new dialog to document
	 * 
	 * @param newShape
	 * @param parent
	 * @param bounds
	 */
	public CreateDialogCommand(Dialog newShape, Document parent,
			Rectangle bounds) {
		this.newDialog = newShape;
		this.parentDocument = parent;
		this.bounds = bounds;
		setLabel("Create Dialog"); //$NON-NLS-N$
	}

	/**
	 * Can execute if all the necessary information has been provided.
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	@Override
	public boolean canExecute() {
		return newDialog != null && parentDocument != null && bounds != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	@Override
	public void execute() {
		newDialog.setLocation(bounds.getLocation());
		Dimension size = bounds.getSize();
		if (size.width > 0 && size.height > 0)
			newDialog.setSize(size);
		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	@Override
	public void redo() {
		parentDocument.addDialog(newDialog);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	@Override
	public void undo() {
		parentDocument.removeDialog(newDialog);
	}

}
